package com.zyzd.custom.widget.cardbagview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Space;


import com.bumptech.glide.Glide;
import com.zyzd.custom.R;
import com.zyzd.custom.util.IdUtils;
import com.zyzd.custom.util.TDevice;

import java.util.ArrayList;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by lzy on 2018/5/7 0007.
 * description ${TODO}
 * <p>
 * Changed by somebody on 2018/5/7 0007
 * description ${TODO}
 */
public abstract class BaseCardBagView<T extends BaseItemBean> extends ConstraintLayout {

    private int mContentViewMarginTop;
    private int mAnchorMarginBottom;
    protected boolean mIsShowIcon;
    protected int mIconSize;
    protected Drawable mIconRes;
    private LayoutInflater mInflater;
    protected ArrayList<View> mIconViews = new ArrayList<>();
    protected ArrayList<View> mContentViews = new ArrayList<>();
    protected ArrayList<T> mDatas = new ArrayList<>();

    public BaseCardBagView(Context context) {
        this(context, null);
    }

    public BaseCardBagView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public BaseCardBagView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mInflater = LayoutInflater.from(context);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BaseCardBagView, defStyleAttr, 0);
        mAnchorMarginBottom = (int) a.getDimension(R.styleable.BaseCardBagView_anchorMarginBottom, 0);
        mIsShowIcon = a.getBoolean(R.styleable.BaseCardBagView_isShowIcon, true);
        mIconSize = (int) a.getDimension(R.styleable.BaseCardBagView_iconSize, TDevice.dpToPx(40));
        mContentViewMarginTop = (int) a.getDimension(R.styleable.BaseCardBagView_contentViewMarginTop, TDevice.dpToPx(40));
        mIconRes = a.getDrawable(R.styleable.BaseCardBagView_icon);
        a.recycle();
    }



    public void setData(ArrayList<T> lists) {

        this.removeAllViews();
        this.mIconViews.clear();
        this.mContentViews.clear();
        this.mDatas.clear();

        if (lists == null || lists.size() == 0) return;
        this.mDatas.addAll(lists);


        int anchorId = getId();
        for (int i = 0; i < mDatas.size(); i++) {
            T itemBean = mDatas.get(i);

            //添加每模块卡片
            View contentView = mInflater.inflate(getContentViewLayoutId(), null);

            int contentViewId = IdUtils.generateViewId();
            contentView.setId(contentViewId);
            contentView.setBackgroundResource(itemBean.getBackRes());
            initContentView(contentView, itemBean, i);

            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            lp.leftToLeft = getId();
            lp.rightToRight = getId();
            lp.topToTop = anchorId;
            lp.setMargins(0, mContentViewMarginTop, 0, 0);
            lp.goneTopMargin = 0;

            mContentViews.add(contentView);
            addView(contentView, lp);

            //添加每模块顶部的icon
            ImageView ivIcon = new ImageView(getContext());
            ivIcon.setVisibility(mIsShowIcon ? VISIBLE : GONE);
            lp = new LayoutParams(mIconSize, mIconSize);
            lp.leftToLeft = getId();
            lp.rightToRight = getId();
            lp.topToTop = anchorId;
            lp.horizontalBias = 0.5f;
            addView(ivIcon, lp);
            mIconViews.add(ivIcon);
            loadIconImage(ivIcon, itemBean, i);

            //添加锚点
            Space anchor = new Space(getContext());
            anchorId = IdUtils.generateViewId();
            anchor.setId(anchorId);

            lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lp.leftToLeft = getId();
            lp.rightToRight = getId();
            lp.bottomToBottom = contentViewId;
            lp.setMargins(0, 0, 0, mAnchorMarginBottom);
            addView(anchor, lp);

        }
    }

    /**
     * 加载中间图片
     *
     * @param ivIcon
     * @param itemBean
     */
    protected void loadIconImage(ImageView ivIcon, T itemBean, int index) {
        Glide.with(getContext())
                .load(itemBean.getIconUrl())
                .bitmapTransform(new CropCircleTransformation(getContext()))
                .centerCrop()
                .into(ivIcon);
        ConstraintLayout.LayoutParams layoutParams = (LayoutParams) ivIcon.getLayoutParams();
        layoutParams.horizontalBias = 0.35f + 0.2f * index;
        ivIcon.setLayoutParams(layoutParams);
    }

    /**
     * 继承重写，进行卡片内容逻辑处理
     *
     * @param contentView
     * @param itemBean
     * @param index
     */
    protected abstract void initContentView(View contentView, T itemBean, int index);

    /**
     * 继承重写，设置卡片布局id
     *
     * @return
     */
    protected abstract int getContentViewLayoutId();
}
